/*
 * Copyright (c) 2010-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/opengl/GLVertexBuffer.h"
#include "alkes/opengl/GLGraphics.h"
#include "alkes/opengl/GLState.h"

namespace alkes {

const static VertexTypeTrait s_table[] = {
    {AL_VERTEX_XY|AL_VERTEX_XYZ,    sizeof(float)   * 3},
    {AL_VERTEX_COLOR,               sizeof(uint8_t) * 4},
    {AL_VERTEX_UV1,                 sizeof(float)   * 2},
    //{AL_VERTEX_UV2,                 sizeof(float)   * 2},
    //{AL_VERTEX_UV3,                 sizeof(float)   * 2},
    //{AL_VERTEX_UV4,                 sizeof(float)   * 2},
    {AL_VERTEX_NORMAL,              sizeof(float)   * 3},
};

GLVertexBuffer* GLVertexBuffer::create(uint32_t vertex_type)
{
    return new GLVertexBuffer(vertex_type);
}

void GLVertexBuffer::destroy()
{
    delete this;
}

GLVertexBuffer::GLVertexBuffer(uint32_t vertex_type)
: VertexBufferHelper(vertex_type)
, vbuffer_(0)
, vbuffer_size_(0)
, vertex_size_(VertexBufferHelper::getVertexSize(s_table, sizeof(s_table)/sizeof(s_table[0]), vertex_type))
, vertex_count_(0)
{
}

GLVertexBuffer::~GLVertexBuffer()
{
    if (vbuffer_)
    {
        AL_OGL_INVOKE(glDeleteBuffers(1, &vbuffer_));
    }
}

int32_t GLVertexBuffer::getVertexCount()
{
    return vertex_count_;
}

void* GLVertexBuffer::getBuffer(int32_t index, int32_t count)
{
    AL_PRE_COND(0 <= index && 0 < count, 0);

    uint32_t buflen = (index + count) * vertex_size_;

    void* p = buffer_.getBuffer(buflen);

    if (p)
        vertex_count_ = index + count;

    return p ? index * vertex_size_ + (uint8_t*)p : 0;
}

int32_t GLVertexBuffer::getVertexSize()
{
    return vertex_size_;
}

const VertexTypeTrait* GLVertexBuffer::getVertexTypeTraits(int32_t& length)
{
    length = sizeof(s_table)/sizeof(s_table[0]);
    return s_table;
}

GLuint GLVertexBuffer::getGLBuffer() const
{
    return vbuffer_;
}

bool GLVertexBuffer::setupVertex(GLGraphics* graphics, GLState& state, uint32_t& start, uint32_t& count)
{
    if (!flag(AL_VBOFLAG_GENERATED))
    {
        graphics->genBuffers(1, &vbuffer_);
        setFlag(AL_VBOFLAG_GENERATED, true);
    }

    return onSetupVertex(graphics, state, start, count);
}

bool GLVertexBuffer::onSetupVertex(GLGraphics* graphics, GLState& state, uint32_t& start, uint32_t& count)
{
    bool modified;

    // Disable VBO
    graphics->bindBuffer(GL_ARRAY_BUFFER, 0, modified);

    bool ret = setupVertexArray(graphics, state, getBuffer(start, count));

    start = 0;

    return ret;
}

bool GLVertexBuffer::setupVertexArray(GLGraphics* /*graphics*/, GLState& state, const void* ptr)
{
#if !defined(ALKES_GLES2)

    uint32_t vtype = getVertexType();
    uint32_t stride = getVertexSize();
    const uint8_t* p = (const uint8_t*)ptr;

    if (vtype & (AL_VERTEX_XYZ|AL_VERTEX_XY))
    {
        state.enable(AL_OGL_ST_VERTEX_ARRAY);
        AL_OGL_INVOKE(glVertexPointer(3, GL_FLOAT, stride, p + getOffsetOf(AL_VERTEX_XYZ)));
    }
    else
    {
        state.disable(AL_OGL_ST_VERTEX_ARRAY);
    }

    if (vtype & AL_VERTEX_COLOR)
    {
        state.enable(AL_OGL_ST_COLOR_ARRAY);
        AL_OGL_INVOKE(glColorPointer(4, GL_UNSIGNED_BYTE, stride, p + getOffsetOf(AL_VERTEX_COLOR)));
    }
    else
    {
        state.disable(AL_OGL_ST_COLOR_ARRAY);
        AL_OGL_INVOKE(glColor4f(1.0f, 1.0f, 1.0f, 1.0f));
    }

    if (vtype & AL_VERTEX_UV1)
    {
        state.enable(AL_OGL_ST_TEXTURE_COORD_ARRAY);
        AL_OGL_INVOKE(glTexCoordPointer(2, GL_FLOAT, stride, p + getOffsetOf(AL_VERTEX_UV1)));
    }
    else
    {
        state.disable(AL_OGL_ST_TEXTURE_COORD_ARRAY);
    }

    return true;

#else
    (void)state;
    (void)ptr;

    return false;
#endif

}

}
